package com.xbao.service.impl;

import com.xbao.dao.OperLogMapper;
import com.xbao.enums.EnumUtil;
import com.xbao.enums.OperLogEnum;
import com.xbao.model.OperLog;
import com.xbao.model.vo.EmpVo;
import com.xbao.service.EmpService;
import com.xbao.service.OperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by qingshengzheng on 2019/1/28.
 */
@Service
public class OperLogServiceImpl implements OperLogService {
    @Autowired
    private OperLogMapper operLogMapper;
    @Resource
    private EmpService empService;


    public String generatorDesc(OperLog log, HashMap<String, String> params) {
        EmpVo vo = empService.detailEmp(log.getEid());
        String template = EnumUtil.getByCode(OperLogEnum.class, log.getBustype() + "").getDesc();
        template.replace("${ename}", vo.getName());
        if (params != null && !params.isEmpty()) {
            Iterator iterator = params.keySet().iterator();
            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                String rp = "${" + key + "}";
                template.replace(rp, params.get(key));
            }
        }
        return template;
    }

    @Override
    public void addOperLog(OperLog operLog, HashMap<String, String> params) {
        String desc = generatorDesc(operLog, params);
        operLog.setDescription(desc);
        operLogMapper.insert(operLog);

    }


}
